import Vue from "vue";
import VueRouter from "vue-router";
import jwt from "jsonwebtoken";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

Vue.use(VueRouter);

// 同步加载
// import Login from '../views/Login.vue';
// import Register from '../views/Register.vue';
// import App from '../views/App.vue';
// import Project from '../views/Project.vue';

// 异步加载（懒加载）
const Login = () => import("../views/Login.vue");
const Register = () => import("../views/Register.vue");
const App = () => import("../views/App.vue");
const Project = () => import("../views/Project.vue");
const Purchasing = () => import("../views/Purchasing.vue");
const Remittance = () => import("../views/Remittance.vue");
const Order = () => import("../views/Order.vue");
const Orderdetailed = () => import("../views/Orderdetailed.vue");
const Balance = () => import("../views/Balance.vue");
const Reconciliation = () => import("../views/Reconciliation.vue");
const Support = () => import("../views/Support.vue");


const router = new VueRouter({
  linkActiveClass: "active",
  routes: [
    { path: "/", redirect: "/login" },
    { path: "/login", component: Login },
    { path: "/register", component: Register },
    {
      path: "/app",
      component: App,
      meta: { requireAuth: true },
      children: [
        { path: "", redirect: "project" },
        { path: "project", component: Project, meta: { requireAuth: true } },
        { path: "purchasing", component: Purchasing, meta: { requireAuth: true } },
        { path: "remittance", component: Remittance, meta: { requireAuth: true } },
        { path: "order", component: Order, meta: { requireAuth: true } },
        { path: "orderdetailed", component: Orderdetailed, meta: { requireAuth: true } },
        { path: "balance", component: Balance, meta: { requireAuth: true } },
        { path: "reconciliation", component: Reconciliation, meta: { requireAuth: true } },
        { path: "support", component: Support, meta: { requireAuth: true } },
      ]
    }
  ]
});


router.beforeEach((to, from, next) => {
  NProgress.start();

  const token = localStorage.getItem("userInfo");
  if (to.path === "/login") {
    next();
  } else if (token) {
    next();
  }
});

router.afterEach(() => NProgress.done());

export default router;
